#ifndef _MAINDLG_H
#define _MAINDLG_H

#include <vector>

#include "..\duilib\UIlib.h"
#include "ITab.h"
#include "LoginDlg.h"

using namespace DuiLib;

class MainDlg : public CWindowWnd
	, public INotifyUI
	, public IMessageFilterUI
{
public:
	LPCTSTR GetWindowClassName()const
	{
		return TEXT("MainDlg");
	}
	UINT GetClassStyle() const 
	{ 
		return UI_CLASSSTYLE_DIALOG; 
	};
	void OnFinalMessage(HWND /*hWnd*/) 
	{ 
		m_pm.RemovePreMessageFilter(this);
		delete this; 
	};

	void OnTabChange(CControlUI* p);

	void Init();

	void Notify(TNotifyUI& msg)
	{
		if(msg.sType == _T("click"))
		{
			if(msg.pSender->GetName() == _T("BtnClose"))
			{
				PostQuitMessage(0L);
				return;
			}
			else if(msg.pSender->GetName() == _T("BtnMin"))
			{
				CloseWindow(m_hWnd);
			}
		}
		if(msg.sType == _T("setfocus"))
		{
			if(_tcsicmp(msg.pSender->GetClass() ,_T("TextUI")) == 0)
			{
				m_pCurSelTag->SetAttribute(_T("bkcolor"), _T("#FF2D2D30"));
				msg.pSender->SetAttribute(_T("bkcolor"), _T("#00FFFFFF"));
				m_pCurSelTag = msg.pSender;
				OnTabChange(m_pCurSelTag);
			}
		}
	}

	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

	LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		if( ::IsIconic(*this) ) bHandled = FALSE;
		return (wParam == 0) ? TRUE : FALSE;
	}

	LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		return 0;
	}

	LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		return 0;
	}

	LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
		::ScreenToClient(*this, &pt);

		RECT rcClient;
		::GetClientRect(*this, &rcClient);

		RECT rcCaption = m_pm.GetCaptionRect();
		if( pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
			&& pt.y >= rcCaption.top && pt.y < rcCaption.bottom ) {
				CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
				if( pControl && _tcscmp(pControl->GetClass(), _T("ButtonUI")) != 0 )
					return HTCAPTION;
		}

		return HTCLIENT;
	}

	LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{

		return 0;
	}

	LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		LRESULT lRes = 0;
		BOOL bHandled = TRUE;
		switch( uMsg ) {
		case WM_CREATE:        lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
		case WM_NCACTIVATE:    lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
		case WM_NCCALCSIZE:    lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
		case WM_NCPAINT:       lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
		case WM_NCHITTEST:     lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
		case WM_SIZE:          lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
		default:
			bHandled = FALSE;
		}
		if( bHandled ) return lRes;
		if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
		return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	}

	LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
	{
		return 0;
	}

	~MainDlg()
	{
		std::vector<ITab*>::iterator it = m_vTabs.begin();
		for(; it != m_vTabs.end(); ++it)
		{
			delete *it;
		}
	}


private:
	CPaintManagerUI m_pm;
	CControlUI* m_pCurSelTag;
	ITab* m_pCurTab;
	CContainerUI* m_pTabCon;
	CContainerUI* m_pMenuCon;
	std::vector<ITab*> m_vTabs;
};

#endif